<style>
    #febs-team-common {
        .jhg-body-search .layui-input-inline{
            width: 180px;
        }
    }
</style>
<div class="layui-fluid layui-anim febs-anim" id="febs-team-common" lay-title="检查小组选择" >
    <div class="layui-row layui-col-space8 febs-container"style="height:calc( 100% - 10px)">

        <div class="layui-col-md12 layui-col-sm12 layui-col-xs12" id="teamTableContainer" style="height:100%">
            <div class="layui-card">
                <div class="layui-card-body febs-table-full">
                    <form class="layui-form layui-table-form" id="team-table-common-form" lay-filter="user-table-common-form">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label layui-form-label-sm" style="width: auto !important;">小组名称</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="teamName" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label layui-form-label-sm" style="width: auto !important;">小组成员</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="realName" autocomplete="off" class="layui-input">
                                </div>
                            </div>

                            <div class="layui-inline">
                                <div id="query" class="layui-btn">
                                    <i class="layui-icon">&#xe615;</i> 查询
                                </div>
                            </div>
                        </div>
                    </form>
                    <div class="jhg-body-table">
                        <table lay-filter="teamTableCommon" lay-data="{id: 'teamTable'}"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script data-th-inline="none" type="text/javascript">
    layui.use(['dropdown', 'jquery', 'laydate', 'commonJS', 'form', 'table', 'febs'], function () {
        var $ = layui.jquery,
            febs = layui.febs,
            form = layui.form,
            table = layui.table,
            $view = $('#febs-team-common'),
            $query = $view.find('#query'),
            $searchForm = $view.find('#team-table-common-form'),
            tableIns;

        let tenantId =POPUP_DATA.tenantId;
        let selectList = POPUP_DATA.selectList
        form.render();

       $view.find("#teamTableContainer").css("width","100%");

        initTable();
        autoHeight()

        function initTable() {
            tableIns = febs.table.init({
                elem: $view.find('table'),
                id: 'teamTableCommon',
                skin: '',
                where:getQueryParams(),
                cols: [[
                    {type: 'checkbox'},
                    {field: 'examineTeamId', title: '主键',hide:true},
                    {field: 'tenantId', title: '公司Id',hide:true},
                    {field: 'teamName', title: '小组名称', minWidth: 180},
                    {field: 'teamUserList', title: '小组成员', minWidth: 500, templet: function (d) {
                            var flg = '';
                            var users = d.teamUserList;
                            if(users){
                                users.forEach((item, index)=> {
                                    flg += '<span class="layui-badge febs-bg-orange" style="margin-left:5px; ">'+item.realName+'</span>'
                                })
                            }
                            return flg;
                        }},
                    {field: 'remark', title: '备注', minWidth: 120},
                    {field: 'sort', title: '排序', width: 80,align:'center'},
                ]],
            });
            tableIns.reload({url: ctx + 'hazardExamineTeam/list', where: getQueryParams(), page: {curr: 1}});
        }


        $query.on('click', function () {
            tableIns.reload({where: getQueryParams(), page: {curr: 1}});
        });


        function getQueryParams() {
            return {
                tenantId:tenantId,
                teamName: $searchForm.find('input[name="teamName"]').val()?.trim(),
                realName: $searchForm.find('input[name="realName"]').val()?.trim(),
                invalidate_ie_cache: new Date()
            };
        }

        function autoHeight() {
            $view.find('.layui-card').height("100%");
        }

        $(window).resize(function () {
            autoHeight();
        });

        window.getSelectData = function(){
            let selData= table.checkStatus("teamTableCommon")
            let returnData = []
            if(selData.data.length>0){
                $.each(selData.data,function(index,item){
                    let userItem = null
                    userItem = {
                        examineTeamId:item.examineTeamId,
                        teamName:item.teamName,
                        teamUserList:item.teamUserList,
                    }
                    if(null != selectList && valueExists(selectList,"examineTeamId",item.examineTeamId)){
                        userItem = null
                    }
                    if(null != selectList && valueExists(selectList,"teamName",item.teamName)){
                        userItem = null
                    }
                    if(null != userItem){
                        returnData.push(userItem)
                    }
                })
            }

            return returnData
        }

        function valueExists (arrayList,fieldName,value){
           let result = $.grep(arrayList,function (e){
                return e[fieldName] == value;
            })
            return result && result.length > 0;
        }
    })
</script>